package cn.micro.system.manage.controller;


import cn.micro.system.manage.annotate.UniqueMenu;
import cn.micro.system.manage.common.log.annotation.Log;
import cn.micro.system.manage.common.log.enums.BusinessType;
import cn.micro.system.manage.common.result.Result;
import cn.micro.system.manage.common.vaildate.ValidateGroup;
import cn.micro.system.manage.entity.SysDimMenu;
import cn.micro.system.manage.entity.vo.AssginMenuVo;
import cn.micro.system.manage.service.SysDimMenuService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author wr
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/manage/sysDimMenu")
@Validated
@RequiredArgsConstructor
public class SysDimMenuController {

    private final SysDimMenuService sysDimMenuService;

    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/doAssign")
    @Log(title = "菜单管理--给角色分配菜单权限",businessType = BusinessType.ASSGIN)
    @PreAuthorize("hasAuthority('bnt.sysRole.assignAuth')")
    public Result doAssign(@RequestBody AssginMenuVo assginMenuVo) {
        sysDimMenuService.doAssign(assginMenuVo);
        return Result.success();
    }

    //根据角色分配菜单
    @ApiOperation("根据角色获取菜单")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable String roleId) {
        List<SysDimMenu> list = sysDimMenuService.findMenuByRoleId(roleId);
        return Result.success(list);
    }

    //菜单列表（树形）
    @ApiOperation("菜单列表")
    @GetMapping("findNodes")
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    public Result findNodes() {
        List<SysDimMenu> list = sysDimMenuService.findNodes();
        return Result.success(list);
    }

    //添加菜单
    @ApiOperation("添加菜单")
    @PostMapping("save")
    @Log(title = "菜单管理--添加菜单",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysMenu.add')")
    public Result save(@Validated(value = ValidateGroup.add.class) @UniqueMenu @RequestBody SysDimMenu sysMenu) {
        if (!sysDimMenuService.save(sysMenu)){
            return Result.fail();
        }
        return Result.success();
    }

    //根据id查询
    @ApiOperation("根据id查询菜单")
    @GetMapping("findNode/{id}")
    @PreAuthorize("hasAuthority('bnt.sysMenu.list')")
    public Result findNode(@PathVariable String id) {
        SysDimMenu sysMenu = sysDimMenuService.getById(id);
        return  Result.success(sysMenu);
    }

    //修改
    @ApiOperation("修改菜单")
    @PutMapping("edit")
    @Log(title = "菜单管理--修改菜单",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.update')")
    public Result edit(@Validated(value = ValidateGroup.edit.class) @UniqueMenu  @RequestBody SysDimMenu sysMenu) {
        if (!sysDimMenuService.updateById(sysMenu)){
            return Result.fail();
        }
        return Result.success();
    }

    //删除菜单
    @ApiOperation("删除菜单")
    @DeleteMapping("remove/{id}")
    @Log(title = "菜单管理--删除菜单",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysMenu.remove')")
    public Result remove(@PathVariable String id) {
        sysDimMenuService.removeMenuById(id);
        return Result.success();
    }
}
